/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.facedoor.modular.deviceset.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.deviceset.entity.DwDeviceSet;
import vip.xiaonuo.facedoor.modular.deviceset.enums.DwDeviceSetExceptionEnum;
import vip.xiaonuo.facedoor.modular.deviceset.mapper.DwDeviceSetMapper;
import vip.xiaonuo.facedoor.modular.deviceset.param.DwDeviceSetParam;
import vip.xiaonuo.facedoor.modular.deviceset.service.DwDeviceSetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.facedoor.iservice.IDeviceSetService;
import vip.xiaonuo.facedoor.modular.deviceset.vo.DeviceSetVo;
import vip.xiaonuo.facedoor.rule.DwRulesEngine;

import javax.annotation.Resource;
import java.util.List;

import vip.xiaonuo.facedoor.rule.HQRulesEngine;
import vip.xiaonuo.facedoor.rule.HTRulesEngine;
import vip.xiaonuo.facedoor.rule.VeinRulesEngine;

/**
 * 网点设置表service接口实现类
 *
 * @author 胡鹏达
 * @date 2022-07-09 09:48:37
 */
@Service
@Slf4j
public class DwDeviceSetServiceImpl extends ServiceImpl<DwDeviceSetMapper, DwDeviceSet> implements DwDeviceSetService {

    @Autowired
    private DeviceBaseService deviceBaseService;

    @Override
    public List<DwDeviceSet> list(DwDeviceSetParam dwDeviceSetParam) {
        return this.list();
    }

    @Override
    public void add(DwDeviceSetParam dwDeviceSetParam) {
        DwDeviceSet dwDeviceSet = new DwDeviceSet();
        BeanUtil.copyProperties(dwDeviceSetParam, dwDeviceSet);
        this.save(dwDeviceSet);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(DwDeviceSetParam dwDeviceSetParam) {
        DwDeviceSet dwDeviceSet = this.queryDwDeviceSet(dwDeviceSetParam);
        BeanUtil.copyProperties(dwDeviceSetParam, dwDeviceSet);
        this.updateById(dwDeviceSet);
    }

    @Override
    public DeviceSetVo detail(DwDeviceSetParam dwDeviceSetParam) {
        DwDeviceSet deviceSet = this.getOne(new LambdaQueryWrapper<DwDeviceSet>()
                .eq(DwDeviceSet::getDeviceBaseId, dwDeviceSetParam.getDeviceBaseId()));
        DeviceBase deviceBase = deviceBaseService.getById(dwDeviceSetParam.getDeviceBaseId());
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在!");
        }
        DeviceSetVo result = new DeviceSetVo();
        if (deviceSet != null) {
            BeanUtil.copyProperties(deviceSet, result);
        }
        if (StringUtil.equals(deviceBase.getDeviceType(), "5")) {
            result.setMaxUseNum(deviceBase.getMaxUseNum());
            result.setAdminPwd(deviceBase.getAdminPwd());
        }
        return result;
    }

    @Override
    public void saveOrUpdate(DwDeviceSetParam dwDeviceSetParam) {
        DeviceBase deviceBase = deviceBaseService.getById(dwDeviceSetParam.getDeviceBaseId());
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在!");
        }
        // 6 海清门禁
        if (ObjectUtil.equal(deviceBase.getDeviceType(), "7")) {
            HQRulesEngine.deviceSetService.saveOrUpdate(dwDeviceSetParam);
        } else if (ObjectUtil.equal(deviceBase.getDeviceType(), "10")) { // 鸿图平台
            HTRulesEngine.deviceSetService.saveOrUpdate(dwDeviceSetParam);
        }else if (ObjectUtil.equal(deviceBase.getDeviceType(), "11")) { // 鸿图平台
            deviceBase.setCabinetIp(dwDeviceSetParam.getIp());
            deviceBase.setCabinetPort(dwDeviceSetParam.getCabinetPort());
            deviceBaseService.updateById(deviceBase);
        }
        else{
            DwRulesEngine.deviceSetService.saveOrUpdate(dwDeviceSetParam);
        }
    }

    @Override
    public void restart(String deviceId) {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在!");
        }
        // 6 海清门禁
        if (ObjectUtil.equal(deviceBase.getDeviceType(), "7")) {
            HQRulesEngine.deviceSetService.restart(deviceId);
        } else if (ObjectUtil.equal(deviceBase.getDeviceType(), DeviceCategory.SUB_TYPE_PALM + "")) { // 掌静脉设备
            VeinRulesEngine.deviceSetService.restart(deviceId);
        } else {
            DwRulesEngine.deviceSetService.restart(deviceId);
        }
    }

    /**
     * 获取网点设置表
     *
     * @author 胡鹏达
     * @date 2022-07-09 09:48:37
     */
    private DwDeviceSet queryDwDeviceSet(DwDeviceSetParam dwDeviceSetParam) {
        DwDeviceSet one = this.getOne(new LambdaQueryWrapper<DwDeviceSet>()
                .eq(DwDeviceSet::getDeviceBaseId, dwDeviceSetParam.getDeviceBaseId()));
        if (ObjectUtil.isNull(one)) {
            throw new ServiceException(DwDeviceSetExceptionEnum.NOT_EXIST);
        }
        return one;
    }
}
